package by.dkusch.aircompany.webapp.page.base;

import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

import by.dkusch.aircompany.model.tables.Crew;
import by.dkusch.aircompany.model.tables.Flight;
import by.dkusch.aircompany.model.tables.Person;
import by.dkusch.aircompany.model.tables.Plane;
import by.dkusch.aircompany.model.tables.PlaneType;
import by.dkusch.aircompany.model.tables.Route;
import by.dkusch.aircompany.model.tables.UserGrant;
import by.dkusch.aircompany.model.tables.UserProfile;
import by.dkusch.aircompany.model.tables.base.BaseEntity;
import by.dkusch.aircompany.webapp.page.base.enums.ActionType;
import by.dkusch.aircompany.webapp.page.flight.panel.FlightEditPanel;
import by.dkusch.aircompany.webapp.page.flight.panel.RouteEditPanel;
import by.dkusch.aircompany.webapp.page.staff.panel.CrewEditPanel;
import by.dkusch.aircompany.webapp.page.staff.panel.PersonEditPanel;
import by.dkusch.aircompany.webapp.page.tech.panel.PlaneEditPanel;
import by.dkusch.aircompany.webapp.page.tech.panel.PlaneTypeEditPanel;
import by.dkusch.aircompany.webapp.page.users.panel.GrantEditPanel;
import by.dkusch.aircompany.webapp.page.users.panel.ProfileEditPanel;

@AuthorizeInstantiation({ "sign_in" })
public class BaseDetailLayout<ID, Entity extends BaseEntity<ID>> extends BaseLayout {
	private static final long serialVersionUID = 1L;
	private ActionType action;
	private BaseEntity<ID> parent;
	private PageCreator pageCreator;
	private Class<?> listItemClass;
	private String currentPage;
	
	@Override
	protected void onInitialize() {
		super.onInitialize();
		if (parent == null) {
			add(new ErrorDetailPanel("detail-panel"));
		}
		if (parent instanceof Flight) {
			addFlightPanel();
		}
		if (parent instanceof Crew) {
			addCrewPanel();
		}
		if (parent instanceof Person) {
			addPersonPanel();
		}
		if (parent instanceof Route) {
			addRoutePanel();
		}
		if (parent instanceof Plane) {
			addPlanePanel();
		}
		if (parent instanceof UserProfile) {
			if (listItemClass == null) {
			addProfilePanel();
			} else {
				if (listItemClass.equals(UserGrant.class)) {
					addGrantPanel();
				}
			}
		}

		if (parent instanceof PlaneType) {
			addPlaneTypePanel();
		}

	}

	@Override
	protected IModel<String> getPageTitle() {
		return new ResourceModel(currentPage);
	}

	private void addFlightPanel() {
		add(new FlightEditPanel("detail-panel", (Flight) parent, pageCreator, action));
		currentPage = "p.flight.title.detail";
	}

	private void addCrewPanel() {
		add(new CrewEditPanel("detail-panel", (Crew) parent, pageCreator, action));
		currentPage = "p.crew.title.detail";
	}

	private void addPersonPanel() {
		add(new PersonEditPanel("detail-panel", (Person) parent, pageCreator, action));
		currentPage = "p.person.title.detail";
	}

	private void addRoutePanel() {
		add(new RouteEditPanel("detail-panel", (Route) parent, pageCreator, action));
		currentPage = "p.route.title.detail";
	}

	private void addPlanePanel() {
		add(new PlaneEditPanel("detail-panel", (Plane) parent, pageCreator, action));
		currentPage = "p.plane.title.detail";
	}

	private void addPlaneTypePanel() {
		add(new PlaneTypeEditPanel("detail-panel", (PlaneType) parent, pageCreator, action));
		currentPage = "p.planetype.title.detail";
	}

	private void addProfilePanel() {
		add(new ProfileEditPanel("detail-panel", (UserProfile) parent, pageCreator, action));
		currentPage = "p.userprofiles.title.detail";
	}

	private void addGrantPanel() {
		add(new GrantEditPanel("detail-panel", (UserProfile) parent, pageCreator, action));
		currentPage = "p.usergrants.title.detail";
	}

	public BaseDetailLayout(Entity parent, ActionType action, PageCreator pageCreator) {
		this(parent, action, pageCreator, null);
	};

	public BaseDetailLayout(Entity parent, ActionType action, PageCreator pageCreator, Class<?> listItemClass) {
		this.action = action;
		this.parent = parent;
		this.pageCreator = pageCreator;
		this.listItemClass = listItemClass;
	};

}